//
//  MyCouponModel.m
//  FD
//
//  Created by Yasin on 2018/10/22.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  卡券model

#import "MyCouponModel.h"

@implementation MyCouponModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"couponId":@"id"};
}

- (NSString *)thumbnail {
    
    if ([_thumbnail hasPrefix:kPicDomain]) {
        return _thumbnail;
    }
    return [NSString stringWithFormat:@"%@%@",kPicDomain,_thumbnail];
}


- (void)setUser:(NSDictionary *)user {
    _user = user;
    
    NSNumber *receive = user[@"receive"];
    if ([receive isEqualToNumber:@1]) { // 0：否 1：是
        _isReceive = YES;
    }else {
        _isReceive = NO;
    }
}



//内部处理
- (void)setActiveDate:(NSNumber *)activeDate {
    _activeDate = activeDate;
    
    NSString *timeStamp = [NSString stringWithFormat:@"%@",activeDate];
    
    //赋值
    _starTimeStr = [self stringFromTimeStamp:timeStamp];
}

- (void)setQuietDate:(NSNumber *)quietDate {
    _quietDate = quietDate;
    
    NSString *timeStamp = [NSString stringWithFormat:@"%@",quietDate];
    
    _endTimeStr = [self stringFromTimeStamp:timeStamp];
}


//时间戳转时间
- (NSString *)stringFromTimeStamp:(NSString *)timeStamp {
    // timeStampString 是服务器返回的13位时间戳
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStamp doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];
    
    return dateString;
}



//判断是否即将过期
#pragma mark - 业务逻辑 判断优惠券是否 ”即将过期“ 11-26
- (BOOL)isBeAboutToExpire {
    
    NSDate *fromDate = [self dateFromTimeStamp:self.quietDate]; //2018-10-13
    NSDate *currentDate = [NSDate date]; //2018-10-20
    
    //计算时间差
    NSInteger day = [self countDaysDistanceWithFromDate:fromDate toDate:currentDate];

    
    
    //绝对值
    int absDay = abs((int)day);
    
    //如果时间差 < 3天 即将过期
    if (absDay < 3) {
        return YES;
    }
    return NO;
}

//用于判断优惠券是否"即将过期"
/**
 1.时间戳转时间---
 2.剩余几天提示优惠券即将过期 暂定 3 天 需求没说
 
 */

/**时间戳转时间 */
- (NSDate *)dateFromTimeStamp:(NSNumber *)timestamp {
    //时间戳--->NSDate
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:([timestamp floatValue] / 1000.0)];

    //    //格式化NSDate
    //    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    //    [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //    //NSDate ---> String
    //    NSString *dateStr = [fmt stringFromDate:date];

    //返回NSDate对象
    return date;
}

/**
 计算两个日期之间的间距
 https://www.jianshu.com/p/2c4a9fc025f3
 
 @param fromDate    开始日期 小日期
 @param toDate 结束日期  大日期
 @return 返回天数差 计算结果可能是 负数
 */
- (NSInteger)countDaysDistanceWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    //创建nNSCalendar
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *components = [calendar components:(NSCalendarUnitDay) fromDate:fromDate toDate:toDate options:(NSCalendarWrapComponents)];
    
    
    FDLog(@"天数差是:%ld",components.day);
    //返回
    return components.day;
}


#pragma mark - 计算cell 的高度
- (CGFloat)cellHeight {
    if (_cellHeight) {
        return _cellHeight;
    }
    
    _cellHeight = 145; //160-15;
    
    //如果即将过期 h + 15
    if (self.isBeAboutToExpire) {
        _cellHeight += 15;
    }
    
    return _cellHeight;
}
@end
